<?php
require_once "ModelBase.php";

class Blog extends TTEvent_Table_Abstract
{
	
	public static function getSesionDB()
	{
		return Zend_Registry::get("session_db");
	}
	
	public function getBlog($nid){
		$nid = (int)$nid;
		$sql = "SELECT * FROM cities WHERE nid=".$nid;
		return $this->execSelectQuery($sql);
	}
	
	public function getEventBlogs($eid, $page=0, $limit=20){
		
		$page = (int)$page;
		$limit = (int)$limit;
		
		$from = $page*$limit;
		$to = ($page+1)*$limit;
		
		$sql="SELECT n.nid, n.title, u.name as uname, T4.last_comment_timestamp, T4.last_comment_name, T4.last_comment_uid, T4.comment_count
				FROM {node} n
				INNER JOIN blog_bookmark g ON n.nid=g.nid AND g.eid=$eid
				LEFT JOIN users u ON n.uid=u.uid
				LEFT JOIN node_comment_statistics T4 ON n.nid=T4.nid
				WHERE n.status=1 AND n.type='ttblog' ORDER BY n.created DESC LIMIT $from, $to";
				
		$db = $this->getSesionDB();
		$rs = $db->query($sql);
		$this->setRowPerPage($limit);
		$db = $this->getSesionDB();
		return $this->pageQuery();
	}
	
	public function getEventBlogCount($eid){
		$eid = (int)$eid;
		$sql = "SELECT COUNT(*) FROM blog_bookmark WHERE type=".EVENT_BLOG_TYPE_EVENT." AND link_id=".$eid;
		
		$db = $this->getSesionDB();
		return $db->fetchOne($sql);
		
	}
	
	public function getDistrictByBlog($cid)
    {
        $db = $this->getSlaveDB();
        $sql = 'SELECT * FROM districts WHERE cid = ?';
        return $db->fetchAll($sql, $cid);   
    }
}

